package com.hmall.controller.back;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hdmall.business.common.annotation.AuthRequired;
import com.hdmall.business.common.common.ServerResponse;
import com.hmall.mapper.*;
import com.hmall.model.ContClause;
import com.hmall.model.ContTempDeptRel;
import com.hmall.model.SysDept;
import com.hmall.vo.ContClauseDept;
import com.hmall.vo.ContractVo;
import com.hmall.vo.DeptVo;
import com.hmall.vo.TemplateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/manage/contract")
@Api(value = "后台合同管理操作API", description = "后台合同管理类API说明")
public class ContractManageController {
    private final
    ContClauseMapper contClauseMapper;

    private final
    ContTempDeptRelMapper contTempDeptRelMapper;

    private final
    MallShopMapper mallShopMapper;

    private final
    SysDeptMapper sysDeptMapper;

    private final
    MallDealerMapper mallDealerMapper;

    @Autowired
    public ContractManageController(ContClauseMapper contClauseMapper, ContTempDeptRelMapper contTempDeptRelMapper, MallShopMapper mallShopMapper, SysDeptMapper sysDeptMapper, MallDealerMapper mallDealerMapper) {
        this.contClauseMapper = contClauseMapper;
        this.contTempDeptRelMapper = contTempDeptRelMapper;
        this.mallShopMapper = mallShopMapper;
        this.sysDeptMapper = sysDeptMapper;
        this.mallDealerMapper = mallDealerMapper;
    }

    @AuthRequired
    @PostMapping("GetContractList")
    @ApiOperation(value = "条款列表")
    public ServerResponse<PageInfo<ContractVo>> GetContractList(Integer dept_id, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                                @RequestParam(value = "pageSize", defaultValue = "100") int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Integer shop_id = mallShopMapper.selectByDeptId(dept_id);
        List<ContractVo> contractManageVoList = contClauseMapper.selectMyContract(shop_id);
        System.out.println("orderManageVoList" + contractManageVoList);
        PageInfo<ContractVo> pageResult = new PageInfo<>(contractManageVoList);
        System.out.println("pageResult" + pageResult);
        return ServerResponse.createBySuccess(pageResult);
    }

    @AuthRequired
    @PostMapping("GetDealerContractList")
    @ApiOperation(value = "办事处条款列表")
    public ServerResponse<PageInfo<ContractVo>> GetDealerContractList(Integer dept_id, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                                      @RequestParam(value = "pageSize", defaultValue = "100") int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Integer dealer_id = mallDealerMapper.selectDealerId(dept_id);
        List<ContractVo> contractManageVoList = contClauseMapper.selectDealerContract(dealer_id);
        System.out.println("orderManageVoList" + contractManageVoList);
        PageInfo<ContractVo> pageResult = new PageInfo<>(contractManageVoList);
        System.out.println("pageResult" + pageResult);
        return ServerResponse.createBySuccess(pageResult);
    }

    @AuthRequired
    @PostMapping("CreatedContract")
    @ApiOperation(value = "添加条款")
    public ServerResponse<String> add(@RequestBody ContClauseDept contClauseDept) {
        ContClause contClause = new ContClause();
        Integer shop_id = mallShopMapper.selectByDeptId(contClauseDept.getDept_id());
        contClause.setIsEnable(contClauseDept.getIsEnable());
        contClause.setContent(contClauseDept.getContent());
        contClause.setTitle(contClauseDept.getTitle());
        contClause.setShopId(shop_id);
        Date date = new Date();
        contClause.setCreateTime(date);
        System.out.println("添加条款:" + contClause.toString());
        contClauseMapper.insert(contClause);
        return ServerResponse.createBySuccess("创建成功");
    }

    @AuthRequired
    @PostMapping("CreatedDealerContract")
    @ApiOperation(value = "添加办事处条款")
    public ServerResponse<String> CreatedDealerContract(@RequestBody ContClauseDept contClauseDept) {
        ContClause contClause = new ContClause();
        Integer shop_id = 1;
        Integer dealer_id = mallDealerMapper.selectDealerId(contClauseDept.getDept_id());
        contClause.setIsEnable(contClauseDept.getIsEnable());
        contClause.setContent(contClauseDept.getContent());
        contClause.setTitle(contClauseDept.getTitle());
        contClause.setShopId(shop_id);
        contClause.setDealerId(dealer_id);
        Date date = new Date();
        contClause.setCreateTime(date);
        System.out.println("添加条款:" + contClause.toString());
        contClauseMapper.insert(contClause);
        return ServerResponse.createBySuccess("创建成功");
    }

    @AuthRequired
    @PostMapping("SaveTemplate")
    @ApiOperation(value = "修改模板")
    public ServerResponse<String> SaveTemplate(@RequestBody TemplateVo templateVo) {
        System.out.println(templateVo.toString());
        List<ContTempDeptRel> oldList = contTempDeptRelMapper.selectById(templateVo.getId());
        for (ContTempDeptRel anOldList : oldList) {
            contTempDeptRelMapper.deleteById(anOldList.getClauseId(), templateVo.getId());
        }
        for (int j = 0; j < templateVo.getChangeList().length; j++) {
            contTempDeptRelMapper.insertClause(templateVo.getId(), templateVo.getChangeList()[j]);
        }
        return ServerResponse.createBySuccess("修改成功");
    }

    @AuthRequired
    @PostMapping("EditContract")
    @ApiOperation(value = "修改条款")
    public ServerResponse EditContract(@RequestBody ContClauseDept contClauseDept) {
        ContClause contClause = contClauseMapper.selectByPrimaryKey(contClauseDept.getClause_id());
        if (contClause == null) {
            return ServerResponse.createByErrorMessage("条款不存在");
        }
        Integer shop_id = mallShopMapper.selectByDeptId(contClauseDept.getDept_id());
        contClause.setIsEnable(contClauseDept.getIsEnable());
        contClause.setContent(contClauseDept.getContent());
        contClause.setTitle(contClauseDept.getTitle());
        contClause.setShopId(shop_id);
        contClause.setCreateTime(contClauseMapper.selectByPrimaryKey(contClauseDept.getClause_id()).getCreateTime());
        Date now = new Date();
        contClause.setUpdateTime(now);
        System.out.println("更新条款:" + contClause.toString());
        int count = contClauseMapper.updateByPrimaryKey(contClause);
        if (contClause.getIsEnable() == 9) {
            contTempDeptRelMapper.enableContract(contClause.getClauseId());
        } else {
            contTempDeptRelMapper.disableContract(contClause.getClauseId());
        }
        if (count == 0) {
            return ServerResponse.createByErrorMessage("修改失败");
        } else {
            return ServerResponse.createBySuccess("修改成功");
        }
    }

    @AuthRequired
    @PostMapping("DisableContract")
    @ApiOperation(value = "禁用条款")
    public ServerResponse DisableContract(Integer id) {
        int count = contClauseMapper.disableContract(id);
        contTempDeptRelMapper.disableContract(id);
        if (count == 0) {
            return ServerResponse.createByErrorMessage("修改失败");
        } else {
            return ServerResponse.createBySuccess("修改成功");
        }
    }

    @AuthRequired
    @PostMapping("EnableContract")
    @ApiOperation(value = "启用条款")
    public ServerResponse EnableContract(Integer id) {
        int count = contClauseMapper.enableContract(id);
        contTempDeptRelMapper.enableContract(id);
        if (count == 0) {
            return ServerResponse.createByErrorMessage("修改失败");
        } else {
            return ServerResponse.createBySuccess("修改成功");
        }
    }

    @AuthRequired
    @PostMapping("GetContracts")
    @ApiOperation(value = "获得条款信息")
    public ServerResponse<ContractVo> GetContract(Integer id) {
        System.out.println("条款ID" + id);
        ContractVo contClause = contClauseMapper.selectContract(id);
        System.out.println(contClause.toString());
        return ServerResponse.createBySuccess(contClause);
    }

    @AuthRequired
    @PostMapping("GetTemplates")
    @ApiOperation(value = "获得模板信息")
    public ServerResponse GetTemplate(Integer id) {
        System.out.println("公司ID" + id);
        List<ContTempDeptRel> relList = contTempDeptRelMapper.selectById(id);
        int[] arr = new int[relList.size()];
        for (int i = 0; i < relList.size(); i++) {
            arr[i] = relList.get(i).getClauseId();
        }
        System.out.println(relList);
        return ServerResponse.createBySuccess(arr);
    }

    @AuthRequired
    @PostMapping("GetMyDeptList")
    @ApiOperation(value = "查询自己的公司")
    public ServerResponse<PageInfo<DeptVo>> GetMyDeptList(Integer dept_id, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                          @RequestParam(value = "pageSize", defaultValue = "100") int pageSize) {
        List<SysDept> deptList = sysDeptMapper.selectMyDept(dept_id);
        List<DeptVo> deptVoList = new ArrayList<>();
        System.out.println(deptList.toString());
        System.out.println(deptList.get(0).getDeptId());
        System.out.println(deptList.size());
        method1(deptList, deptVoList);
        System.out.println(deptVoList.toString());
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<DeptVo> pageResult = new PageInfo<>(deptVoList);
        System.out.println("pageResult" + pageResult);
        return ServerResponse.createBySuccess(pageResult);
    }

    private void method1(List<SysDept> deptList, List<DeptVo> deptVoList) {
        for (SysDept aDeptList : deptList) {
            List<ContClause> contractList = contClauseMapper.selectClauseById(aDeptList.getDeptId());
            System.out.println(contractList.toString());
            DeptVo deptVo = new DeptVo();
            deptVo.setDept_id(aDeptList.getDeptId());
            deptVo.setContractList(contractList);
            deptVo.setName(aDeptList.getName());
            deptVo.setCreate_time(aDeptList.getCreateTime().toLocaleString());
            if (aDeptList.getUpdateTime() != null) {
                deptVo.setUpdate_time(aDeptList.getUpdateTime().toLocaleString());
            }
            System.out.println(deptVo.toString());
            deptVoList.add(deptVo);
        }
    }

    @AuthRequired
    @PostMapping("GetDeptList")
    @ApiOperation(value = "查询所有公司")
    public ServerResponse<PageInfo<DeptVo>> GetDeptList(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                        @RequestParam(value = "pageSize", defaultValue = "100") int pageSize) {
        List<SysDept> deptList = sysDeptMapper.selectALLDept();
        List<DeptVo> deptVoList = new ArrayList<>();
        System.out.println(deptList.size());
        method1(deptList, deptVoList);
        System.out.println(deptVoList.toString());
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<DeptVo> pageResult = new PageInfo<>(deptVoList);
        System.out.println("pageResult" + pageResult);
        return ServerResponse.createBySuccess(pageResult);
    }

    @AuthRequired
    @PostMapping("GetDealerDeptList")
    @ApiOperation(value = "查询办事处所有公司")
    public ServerResponse<PageInfo<DeptVo>> GetDealerDeptList(Integer dept_id, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                              @RequestParam(value = "pageSize", defaultValue = "100") int pageSize) {
        Integer dealer_id = mallDealerMapper.selectDealerId(dept_id);
        List<SysDept> deptList = sysDeptMapper.selectDealerDept(dealer_id);
        List<DeptVo> deptVoList = new ArrayList<>();
        method2(deptList, deptVoList);
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<DeptVo> pageResult = new PageInfo<>(deptVoList);
        return ServerResponse.createBySuccess(pageResult);
    }

    private void method2(List<SysDept> deptList, List<DeptVo> deptVoList) {
        for (SysDept aDeptList : deptList) {
            List<ContClause> contractList = contClauseMapper.selectClauseById(aDeptList.getDeptId());
            DeptVo deptVo = new DeptVo();
            deptVo.setDept_id(aDeptList.getDeptId());
            deptVo.setContractList(contractList);
            deptVo.setName(aDeptList.getName());
            deptVo.setCreate_time(aDeptList.getCreateTime().toLocaleString());
            if (aDeptList.getUpdateTime() != null) {
                deptVo.setUpdate_time(aDeptList.getUpdateTime().toLocaleString());
            }
            deptVoList.add(deptVo);
        }
    }

    @AuthRequired
    @PostMapping("GetSalesManDeptList")
    @ApiOperation(value = "查询业务员所有公司")
    public ServerResponse<PageInfo<DeptVo>> GetSalesManDeptList(Integer user_id, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                                @RequestParam(value = "pageSize", defaultValue = "100") int pageSize) {
        List<SysDept> deptList = sysDeptMapper.selectSalesManDept(user_id);
        List<DeptVo> deptVoList = new ArrayList<>();
        method2(deptList, deptVoList);
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<DeptVo> pageResult = new PageInfo<>(deptVoList);
        return ServerResponse.createBySuccess(pageResult);
    }

    @AuthRequired
    @GetMapping("SearchContract")
    @ApiOperation(value = "搜索条款")
    public ServerResponse<PageInfo<ContClause>> SearchContract(String query, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                               @RequestParam(value = "pageSize", defaultValue = "100") int pageSize) {
        System.out.println(query);
        PageHelper.startPage(pageNum, pageSize);
        List<ContClause> contClauseList = contClauseMapper.selectListByTitle(query);
        PageInfo<ContClause> pageResult = new PageInfo<>(contClauseList);
        return ServerResponse.createBySuccess(pageResult);
    }

    @AuthRequired
    @GetMapping("SearchDealerContract")
    @ApiOperation(value = "搜索办事处条款")
    public ServerResponse<PageInfo<ContClause>> SearchDealerContract(String query, Integer dept_id, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                                     @RequestParam(value = "pageSize", defaultValue = "100") int pageSize) {
        System.out.println(query);
        PageHelper.startPage(pageNum, pageSize);
        Integer dealer_id = mallDealerMapper.selectDealerId(dept_id);
        List<ContClause> contClauseList = contClauseMapper.selectDealerListByTitle(query, dealer_id);
        PageInfo<ContClause> pageResult = new PageInfo<>(contClauseList);
        return ServerResponse.createBySuccess(pageResult);
    }
}
